/*
Copyright (C) Ariff Ehsan, Abdul Hadi Fikri

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*  Author : Ariff Ehsan
 *           Abdul Hadi Fikri
 *  Date   : June 18, 2009
 */


#include <system.h>

/**
*Interface at644p_timer0{
*	Provides{
*		command void AT644P_TIMER0_init();                    @DRIVER_CLOCK_INIT();
*		command void AT644P_TIMER0_reg(void (*fired)(void));  @DRIVER_CLOCK_REG(x);
*	}
*	Uses{
*		signal void(*at644p_timer0_fired)();
*	}
*
*}
*
**
*Data Field:
*/	void (*atmega644p_timer0_fired)(void);

/**
 *Function Member:
 */void ATMEGA644P_TIMER0_defaultIsr(void);

/********************************************************************************************/
/*                                  Clients Function Call                                   */
/********************************************************************************************/
void ATMEGA644P_TIMER0_init(void) {
	/* set default fired function */
	atmega644p_timer0_fired = ATMEGA644P_TIMER0_defaultIsr;
	
	/* set pwm mode - CTC */
	TCCR0A = (1<<WGM01);
	
	/* set prescaller - 64 */
	TCCR0B = (1<<CS01)|(1<<CS00);
	
	/* reset counter */
	TCNT0 = 0;
	
	/* set output compare */
	OCR0A = 125;
	
	/* enable output compare interrupt A */
	TIMSK0 |= (1<<OCIE0A);
}

void ATMEGA644P_TIMER0_reg(void (*fired)(void)) {
  if (fired == NULL) {
	  return;
	}

  atmega644p_timer0_fired = fired;
}
/********************************************************************************************/
/*                                 Internal Function Call                                   */
/********************************************************************************************/
void ATMEGA644P_TIMER0_defaultIsr(void) {
}
/********************************************************************************************/
/*                              HW/SW Interrupt Function Call                               */
/********************************************************************************************/
/**
 *  Timer1 interrupt
 */
ISR(TIMER0_COMPA_vect) {
  (*atmega644p_timer0_fired)();
}


